package com.mimc.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.upload.UploadFile;
import com.mimc.common.constant.AppConstants;
import com.mimc.common.exception.ServiceException;
import com.mimc.common.utils.file.FileUtils;
import com.mimc.common.utils.file.ImageUtils;
import com.mimc.core.model.ImUser;
import com.mimc.core.service.ImUserService;
import com.mimc.framework.jwt.ApiTokenUtils;
import com.mimc.framework.manager.vo.MinioConf;
import com.mimc.framework.utils.ImageUtil;
import com.mimc.framework.utils.ShUtils;
import io.jboot.Jboot;
import io.jboot.aop.annotation.Bean;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

import java.io.File;
import java.util.Date;

@Bean
@Slf4j
public class ShUserService {
    @Inject
    private ShSysConfigService shSysConfigService;
    @Inject
    private ShMinioService shMinioService;
    @Inject
    private ImUserService imUserService;

    public Boolean uploadAvatar(UploadFile file){
        String configValue=shSysConfigService.getConfigValueByKey("upload.settings");
        JSONObject jsonObj= JSON.parseObject(configValue);
        if(file.getFile().length() > AppConstants.MAX_IMAGE_SIZE){
            throw new ServiceException("文件大小不能超过5M");
        }
        byte[] imageByte = ImageUtil.compressForScale(FileUtils.file2byte(file.getFile()),100);
        log.info("getOriginalFileName:{}",file.getOriginalFileName());
        // 图片格式校验
        if(!ImageUtils.isImage(file.getOriginalFileName())){
            throw new ServiceException("图片格式不合法");
        }
        ImUser user= ApiTokenUtils.getCurrentUser();
        if(jsonObj.getString("default").equals("local")){
            //本地上传
            String uploadPath= Jboot.configValue("upload.path");
            String saveName = "avatar/"+user.getUserId()+".png";
            File dest=new File(uploadPath+"/"+saveName);
            //FileUtil.copy(file.getFile(),dest,true);
            FileUtil.writeBytes(imageByte,dest);
            file.getFile().deleteOnExit();
            String url=ShUtils.getLocalConf().getDomain()+"/"+saveName;
            int version=user.getAvatarVer();
            user.setAvatarVer(version+1);
            user.setAvatar(url);
            return imUserService.update(user);
        }else if(jsonObj.getString("default").equals("minio")){
            String saveName = "avatar/"+user.getUserId()+".png";
            String fileName = shMinioService.uploadAvatar(saveName,imageByte);
            file.getFile().deleteOnExit();
            if(fileName==null){
                return false;
            }
            String url=ShUtils.getMinioConf().getDomain()+"/"+saveName;
            int version=user.getAvatarVer();
            user.setAvatarVer(version+1);
            user.setAvatar(url);
            return imUserService.update(user);
        }
        return false;
    }
}
